﻿package com.jimster.plugins.utils
{
	public class MathUtil
	{
		//Gets random number from a discribed range
		public static function randomRange( $min, $max ):Number
		{
			return Math.random() * ( $max - $min ) + $min;
		}
		
		// Convert values to a percent
		public static function toPercent( $curValue:Number, $totalValue:Number ):Number
		{
			return ( $curValue / $totalValue ) * 100;
		}
		
		// Convert values to a percent in a ratio form  0 to 1
		public static function toPercentRatio( $curValue:Number, $totalValue:Number ):Number
		{
			return (( $curValue / $totalValue ) * 100) / 100;
		}
		
		public static function ratioToValue( $percentRatio:Number, $min:Number, $max:Number ):Number
		{
			var _numValue:Number;
			var _numDiffer:Number = $max - $min;
	
			_numValue = ( _numDiffer * $percentRatio ) + $min;
	
			return _numValue;
		}
		
		//Getting a ratio between two numbers.
       public static function returnValue( value:Number, actualMin:Number, actualMax:Number, ratioMin:Number, ratioMax:Number ):Number
	   {
  		   return ( ( ( value - actualMin ) / ( actualMax - actualMin ) ) * ( ratioMax - ratioMin ) ) + ratioMin;
	   }
	   
	   //This version returns a percentage of the two range of numbers.
		public static function returnValuePercentage( _value:Number, min:Number, max:Number ):Number
		{
			return ( ( (  _value - min ) / ( max - min ) ) * 100  );
		}
		
		//Gets the distance between 2 objects
		public static function toDistance( $point1X:Number, $point2X:Number, $point1Y:Number, $point2Y:Number ):Number
		{
			var dx:Number = $point1X - $point2X;
			var dy:Number = $point1Y - $point2Y;
			return Math.sqrt( ( dx * dx ) + ( dy * dy ) );
		}
		
		// Gets the angle of an object in degrees or radians
		public static function toAngle( $centerX:Number, $centerY:Number, $targetX:Number, $targetY:Number, $mathSystem:String="radians" ):Number
		{
			var angle:Number;
			
			if( $mathSystem == "degrees" )
			{
				var radians:Number = Math.atan2( ( $targetY - $centerY ), ( $targetX - $centerX ) );
				angle = MathUtil.toDegrees( radians );
			}
			else
			{
				angle = Math.atan2( ( $targetY - $centerY ), ( $targetX - $centerX ) );
			}
			return angle;
		}
		
		// Covert degrees to radians
		public static function toRadians( $degrees:Number ):Number
		{
			return $degrees * Math.PI / 180;
		}
		
		// Covert radians to degrees
		public static function toDegrees( $radians:Number ):Number
		{
			return $radians * 180 / Math.PI;
		}
		
		//Rounds decimo value
		public static function roundDecimo( $value:Number, $decimoPlace:Number=10 ):Number
		{
			return int( $value * $decimoPlace ) / $decimoPlace;
		}
		
	}
}